<?php


namespace app\admin\controller;

use app\admin\model\User as UserM;
use app\admin\service\User as UserS;

class User extends Base
{

    public function filter(&$map, &$field)
    {
        $field = 'id,nickname,account,create_time,status';
    }
    public function beforeInsert(&$data)
    {
        $data['salt'] = password($data['password']);
        unset($data['password'], $data['repassword']);
    }


    public function beforeUpdate(&$data)
    {
        $data['salt'] = password($data['password']);
        unset($data['password'], $data['repassword']);

        //修改用户信息，会将用户在有效期的token全部加入黑名单，并清空关联关系
        $userS = new UserS();
        $userS->clearTokenAndJoinBlacklist($data['id']);
    }

    public function beforeDetail(&$field)
    {
       $field = 'id,nickname,account,create_time,status';

    }


    public function finishStatus($ids, $status)
    {

        //禁用 或修改密码，需删除此用户在redis中的token缓存，需重新登录。
        if($status != 1)
        {
            //将用户在有效期的token全部加入黑名单，并清空关联关系
            $userS = new UserS();
            foreach($ids as $k=>$v){
                $userS->clearTokenAndJoinBlacklist($v);
            }
        }
    }



    public function finishDel($ids)
    {

        //将用户在有效期的token全部加入黑名单，并清空关联关系
        $userS = new UserS();
        foreach($ids as $k=>$v){
            $userS->clearTokenAndJoinBlacklist($v);
        }
    }


}